<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发票号码" prop="deductionNo">
        <el-input
          v-model="queryParams.deductionNo"
          placeholder="请输入发票号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票时间" prop="invoiceTime">
        <el-date-picker clearable
          v-model="queryParams.invoiceTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发票时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="销方名称" prop="saleName">
        <el-input
          v-model="queryParams.saleName"
          placeholder="请输入销方名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抵扣状态" prop="deductionStatus">
        <el-select v-model="queryParams.deductionStatus" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
          <el-option v-for="item in deductionStatus" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cld:inputDeduction:add']"
        >新增抵扣</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleBatchUpdateOpen"
          v-hasPermi="['cld:inputDeduction:edit']"
        >批量修改抵扣</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inputDeductionList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" :reserve-selection="true"/>
      <el-table-column label="发票号码" align="center" prop="deductionNo" width="200px"/>
      <el-table-column label="发票类型" align="center" prop="invoiceType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_invoice_type" :value="scope.row.invoiceType" />
        </template>
      </el-table-column>
      <el-table-column label="发票时间" align="center" prop="invoiceTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.invoiceTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销方名称" align="center" prop="saleName" width="400px"/>
      <el-table-column label="销方识别号" align="center" prop="saleIdentification" width="200px"/>
      <el-table-column label="金额" align="center" prop="deductionAmount" />
      <el-table-column label="税率" align="center" prop="deductionTax" >
        <template slot-scope="scope">
          <span>{{ scope.row.deductionTax * 100 }}%</span>
        </template>
      </el-table-column>
      <el-table-column label="税额" align="center" prop="deductionTaxAmount" />
      <el-table-column label="价税合计" align="center">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.deductionAmount + scope.row.deductionTaxAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="抵扣状态" align="center" prop="deductionStatus" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.deductionStatus == 0" type="info">未抵扣</el-tag>
          <el-tag v-if="scope.row.deductionStatus == 1" type="success">已抵扣</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="抵扣时间" align="center">
        <template slot-scope="scope" v-if="scope.row.deductionYear != null">
          <span>{{ scope.row.deductionYear + '-' + scope.row.deductionMonth}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人名称" align="center" prop="createName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:inputDeduction:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cld:inputDeduction:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改进项抵扣对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发票号码" prop="deductionNo">
          <el-input v-model="form.deductionNo" placeholder="请输入发票号码" />
        </el-form-item>
        <el-form-item label="发票时间" prop="invoiceTime">
          <el-date-picker clearable
            v-model="form.invoiceTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发票时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发票类型" prop="invoiceType">
          <el-select filterable v-model="form.invoiceType" placeholder="请选择发票类型">
            <el-option v-for="dict in dict.type.cld_invoice_type" :key="dict.value" :label="dict.label"
                       :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销方名称" prop="saleName">
          <el-input v-model="form.saleName" placeholder="请输入销方名称" />
        </el-form-item>
        <el-form-item label="识别号" prop="saleIdentification">
          <el-input v-model="form.saleIdentification" placeholder="请输入销方识别号" />
        </el-form-item>
        <el-form-item label="金额" prop="deductionAmount">
          <el-input type="number" v-model="form.deductionAmount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="税率" prop="deductionTax">
          <el-input type="number" v-model="form.deductionTax" placeholder="请输入税率" />
        </el-form-item>
        <el-form-item label="税额" prop="deductionTaxAmount">
          <el-input type="number" v-model="form.deductionTaxAmount" placeholder="请输入税额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 批量修改抵扣 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="isBacthUpdateOpen" append-to-body title="批量修改抵扣"
               width="400px">
      <el-form :model="form" :rules="rules" label-width="80px" size="mini">
        <el-form-item label="选择月份" prop="months">
          <el-date-picker v-model="form.months" placeholder="选择月" type="month" value-format="yyyy-MM"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isBacthUpdateOpen = false">取 消</el-button>
        <el-button type="primary" @click="handleBatchUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInputDeduction, getInputDeduction, delInputDeduction, addInputDeduction, updateInputDeduction, batchDeduction } from "@/api/cld/inputDeduction";
import {toMoney} from "../../../utils/ruoyi";

export default {
  name: "inputDeduction",
  dicts: ['cld_invoice_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进项抵扣表格数据
      inputDeductionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isBacthUpdateOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deductionNo: null,
        invoiceTime: null,
        saleName: null,
        saleIdentification: null,
        deductionStatus:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deductionNo: [
          { required: true, message: "请输入发票号码", trigger: "blur" },
        ],
        invoiceTime: [
          { required: true, message: "请选择发票时间", trigger: "blur" },
        ],
        invoiceType: [
          { required: true, message: "请选择发票类型", trigger: "blur" },
        ],
        saleName: [
          { required: true, message: "请输入销方名称", trigger: "blur" },
        ],
        saleIdentification: [
          { required: true, message: "请输入销方识别号", trigger: "blur" },
        ],
        deductionAmount: [
          { required: true, message: "请输入金额", trigger: "blur" },
        ],
        deductionTax: [
          { required: true, message: "请输入税率", trigger: "blur" },
        ],
        deductionTaxAmount: [
          { required: true, message: "请输入税额", trigger: "blur" },
        ],
      },
      // 收支类型
      deductionStatus: [{
        value: '1',
        label: '已抵扣'
      }, {
        value: '0',
        label: '未抵扣'
      }],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    toMoney,
    /** 查询进项抵扣列表 */
    getList() {
      this.loading = true;
      listInputDeduction(this.queryParams).then(response => {
        this.inputDeductionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inputDeductionId: null,
        deductionNo: null,
        invoiceType: null,
        invoiceTime: null,
        saleName: null,
        saleIdentification: null,
        deductionAmount: null,
        deductionTax: null,
        deductionTaxAmount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inputDeductionId)
      if(selection.length >= 1){
        this.single = false;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加进项抵扣";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inputDeductionId = row.inputDeductionId || this.ids
      getInputDeduction(inputDeductionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改进项抵扣";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.inputDeductionId != null) {
            updateInputDeduction(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInputDeduction(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inputDeductionIds = row.inputDeductionId || this.ids;
      this.$modal.confirm('是否确认删除进项抵扣编号为"' + inputDeductionIds + '"的数据项？').then(function() {
        return delInputDeduction(inputDeductionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cld/inputDeduction/export', {
        ...this.queryParams
      }, `deduction_${new Date().getTime()}.xlsx`)
    },
    //批量修改
    handleBatchUpdateOpen(){
      this.form = {};
      this.form.inputDeductionIds = this.ids.join(',');
      this.isBacthUpdateOpen = true;
    },
    /** 批量修改抵扣 */
    handleBatchUpdate() {
      if (!this.form.months) {
        this.$message.error('请选择修改月份！');
        return;
      }
      this.$confirm(`是否确认修改[${this.form.months}]的抵扣?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.form.deductionYear = this.parseTime(this.form.months, '{y}');
        this.form.deductionMonth = this.parseTime(this.form.months, '{m}');
        batchDeduction(this.form).then(res => {
          this.isBacthUpdateOpen = false;
          this.$modal.msgSuccess("操作成功");
          this.getList();
        })
      }).catch(() => {
      });
    },
  }
};
</script>
